﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ManagementProject
{

    public partial class Form1 : Form
    {
        bool flag = false;
        bool flagSearch = false; //use for switch Search/Cancel

        public Form1()
        {
            InitializeComponent();

        }
        #region MyCode:
        //add New Employee
        void New()
        {
            btnNew.Enabled = true;
            btnAdd.Enabled = true;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
            txtID.Clear();
            txtLastName.Clear();
            txtFirstName.Clear();
            txtTitle.Clear();
            txtAddress.Clear();
            txtCity.Clear();
            txtRegion.Clear();
            txtPostalCode.Clear();
            txtSearch.Clear();
            cboTitleOfCoutersy.SelectedIndex = 0;
            cboSearch.SelectedIndex = 0;
            mskPhone.Clear();
        }
        void addNewEmployee()
        {
            Random random = new Random();
            int id = random.Next(1, 999);
            txtID.Text = id.ToString();
            try
            {

                gvEmployee.Rows.Add(id, txtLastName.Text, txtFirstName.Text, txtTitle.Text,
                     cboTitleOfCoutersy.Text, dTPDOB.Text, dTPHireDate.Text, txtAddress.Text, cboCountry.Text,
                   txtCity.Text, txtRegion.Text, txtPostalCode.Text, mskPhone.Text);
            }
            catch
            {
            }
        }
        //Update
        void updateEmployee()
        {
            if (gvEmployee.SelectedRows.Count > 0)
            {
                DataGridViewRow r = gvEmployee.SelectedRows[0];
                r.Cells["clLastName"].Value = txtLastName.Text;
                r.Cells["clFirstName"].Value = txtFirstName.Text;
                r.Cells["clDOB"].Value = dTPDOB.Value.ToShortDateString();
                r.Cells["clHireDate"].Value = dTPHireDate.Value.ToShortDateString();
                r.Cells["clCity"].Value = txtCity.Text;
                r.Cells["clAddress"].Value = txtAddress.Text;
                r.Cells["clPostalCode"].Value = txtPostalCode.Text;
                r.Cells["clRegion"].Value = txtRegion.Text;

            }
        }
        //Delete
        void deleteEmployee()
        {

            if (gvEmployee.SelectedRows.Count > 0)
            {
                DataGridViewRow r = gvEmployee.SelectedRows[0];
                gvEmployee.Rows.Remove(r);
            }
        }
        //searchEmployee
        void searchEmployee()
        {
            string searchMethod = cboSearch.Text;
            int id = int.Parse(txtSearch.Text);
            DataGridView view = new DataGridView();
            if (flagSearch == false)
            {
                btnSearch.Text = "Cancel";
                flagSearch = true; //flag for switch to Cancel
                //switch (searchMethod)
                //{
                //    case "by ID":

                //}
            }
            else
            {
                flagSearch = false;
                btnSearch.Text = "Search";
            }

            flagSearch = false; //flag for switch to Search

        }
        //Validation
        bool validateInput()
        {
            string firstname = txtFirstName.Text;
            string lastname = txtLastName.Text;
            string title = txtTitle.Text;
            string address = txtAddress.Text;
            string country = cboCountry.Text;
            bool error = false;
            if (firstname.Length == 0)
            {
                errorProvider1.SetError(txtFirstName, "Please enter your first name!");
                error = true;
            }
            if (lastname.Length == 0)
            {
                errorProvider1.SetError(txtLastName, "Please enter your first name!");
                error = true;
            }
            if (title.Length == 0)
            {
                errorProvider1.SetError(txtTitle, "Please enter your title!");
                error = true;
            }
            if (address.Length == 0)
            {
                errorProvider1.SetError(txtAddress, "Please enter your address!");
                error = true;
            } if (country.Length == 0)
            {
                errorProvider1.SetError(cboCountry, "Please choose your country");
                error = true;
            }

            DateTime currDate = DateTime.Now;
            int currYear = currDate.Year;
            int currDay = currDate.Day;
            int currMonth = currDate.Month;
            DateTime dateOfBirth = dTPDOB.Value;
            int birthYear = dateOfBirth.Year;
            DateTime dateOfHire = dTPHireDate.Value;
            int HireYear = dateOfHire.Year;
            int HireMonth = dateOfHire.Month;
            int HireDay = dateOfHire.Day;
            if (HireYear - birthYear < 18)
            {
                errorProvider1.SetError(dTPDOB, "You must be greater than or equal to 18 for hiring");
                error = true;
            }
            if (HireYear - currYear > 0)
            {
                errorProvider1.SetError(dTPHireDate, "Hire date must be before today");
                error = true;
            }
            else if ((HireYear == currYear) && (HireMonth - currMonth > 0))
            {
                errorProvider1.SetError(dTPHireDate, "Hire date must be before today");
                error = true;
            }
            else if (HireMonth == currMonth && HireDay - currDay > 0)
            {
                errorProvider1.SetError(dTPHireDate, "Hire date must be before today");
                error = true;
            }

            if (mskPhone.MaskCompleted == false)
            {
                errorProvider1.SetError(mskPhone, "Please enter required digit!");
                error = true;
            }
            if (error == true)
            {
                return false;
            }
            else
                errorProvider1.Clear();
            return true;
        }
        #endregion



        private void btnAdd_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (validateInput() == false)
                return;
            addNewEmployee();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            New();
        }

        private void txtLastName_KeyDown(object sender, KeyEventArgs e)
        {
            flag = false;
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    flag = true;
                }
            }
            if (Control.ModifierKeys == Keys.Shift)
            {
                flag = true;
            }
        }

        private void txtLastName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (flag == false)
            {
                e.Handled = true;
            }

        }

        private void txtFirstName_KeyDown(object sender, KeyEventArgs e)
        {
            flag = false;
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    flag = true;
                }
            }
        }

        private void txtFirstName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (flag == false)
            {
                e.Handled = true;
            }
        }

        private void txtPostalCode_KeyDown(object sender, KeyEventArgs e)
        {
            flag = false;
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    if (e.KeyCode != Keys.Back)
                        flag = true;
                }
            }
        }
        private void txtPostalCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (flag == true)
            {
                e.Handled = true;
            }
        }


        private void btnSearch_Click(object sender, EventArgs e)
        {

        }

        private void gvEmployee_SelectionChanged(object sender, EventArgs e)
        {
            New();


            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            btnAdd.Enabled = false;
            try
            {
                {
                    DataGridViewRow r = gvEmployee.SelectedRows[0];
                    txtID.Text = r.Cells[0].Value.ToString();
                    txtFirstName.Text = r.Cells[2].Value.ToString();
                    txtLastName.Text = r.Cells[1].Value.ToString();
                    txtTitle.Text = r.Cells[3].Value.ToString();
                    cboTitleOfCoutersy.Text = r.Cells[4].Value.ToString();
                    string s = r.Cells[5].Value.ToString();
                    for (int i = 0; i < 5; i++)
                    {
                        string a = (string)cboTitleOfCoutersy.Items[i];
                        if (s.Equals(a))
                        {
                            cboTitleOfCoutersy.SelectedIndex = i;
                            break;
                        }
                    }
                    DateTime dt1, dt2;
                    DateTime.TryParse(r.Cells[5].Value.ToString(), out dt1);
                    dTPDOB.Value = dt1;
                    DateTime.TryParse(r.Cells[6].Value.ToString(), out dt2);
                    dTPHireDate.Value = dt2;
                    txtAddress.Text = r.Cells[7].Value.ToString();
                    txtCity.Text = r.Cells[8].Value.ToString();
                    txtRegion.Text = r.Cells[9].Value.ToString();
                    txtPostalCode.Text = r.Cells[10].Value.ToString();
                    cboCountry.Text = r.Cells[11].Value.ToString();
                    mskPhone.Text = r.Cells[12].Value.ToString();
                }
            }
            catch { }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (validateInput())
                updateEmployee();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            deleteEmployee();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }








}

